//
//  NewsDetailsViewController.m
//  news
//
//  Created by lanou on 15/12/17.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "NewsDetailsViewController.h"
#import "NewsDetails.h"
#import "NewsDetailsView.h"

@interface NewsDetailsViewController ()<UIWebViewDelegate>

@property (nonatomic, retain) NewsDetailsView *newsDetailsView;

@end

@implementation NewsDetailsViewController

#pragma mark -
#pragma mark ---网络解析---

- (void)josnNewsDetails:(NSString *)url
{
    LoadingAnimationView *loadingView = [[LoadingAnimationView alloc] init];
    
    [loadingView beginLoadingWithView:self.view];
    
    [LORequestManger GET:url success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        
        NSArray *array = [url componentsSeparatedByString:@"/"];
        
        self.docid = array[5];
        
        self.newsDetailsView.newsDetails = [NewsDetails detailWithDict:dic[self.docid]];
        
        
        
        [self.view addSubview:self.newsDetailsView];
        
        [loadingView endLoading];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark -
#pragma mark ---webView代理方法---


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        
        CATransition *animation = [CATransition animation];
        animation.duration = 0.7f;
        animation.type = @"rippleEffect";
        animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
        [self.view.layer addAnimation:animation forKey:@"animation"];
        NSString *url = [request.URL absoluteString];
        [self josnNewsDetails:url];
        return NO;
    } else {
        return YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.newsDetailsView = [[NewsDetailsView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.newsDetailsView.webView.delegate = self;
    
    __weak NewsDetailsViewController *newsDetailsVC = self;
    
    [self.newsDetailsView.webView.scrollView addFooterWithCallback:^{
        [newsDetailsVC back];
    }];
    self.newsDetailsView.webView.scrollView.footerPullToRefreshText = @"下拉关闭当前页面";
    self.newsDetailsView.webView.scrollView.footerReleaseToRefreshText = @"松开关闭当前页面";
    self.newsDetailsView.webView.scrollView.footerRefreshingText = @"正在关闭当前页面";
    
    
    
    UIView *statusBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 20)];
    statusBG.backgroundColor = self.view.backgroundColor;
    [self.view addSubview:statusBG];
    
    NSString *url = [NSString stringWithFormat:kNewsDetails,self.docid];
    
    [self josnNewsDetails:url];
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = left;
    
     
    // Do any additional setup after loading the view.
}

- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
